package com.zhixue.content.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhixue.content.mapper.*;
import com.zhixue.content.model.dto.*;
import com.zhixue.content.model.po.*;
import com.zhixue.content.service.TeachplanService;
import com.zhixue.exception.ZhixuePlusException;
import com.zhixue.r.R;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @Auther: 李 力
 * @Date: 2025/4/8
 * @Description: com.zhixue.content.service.impl
 * @version: 1.0
 */
@Service
public class TeachplanServiceImpl extends ServiceImpl<TeachplanMapper, Teachplan> implements TeachplanService {

    @Autowired
    private TeachplanMapper teachplanMapper;

    @Autowired
    private TeachplanMediaMapper mediaMapper;

    @Override
    public List<TeachplanDto> getTeachplanInfo(Long courseId) {
        return teachplanMapper.getTeachplanInfo(courseId);

    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void addOrUpdate(SaveTeachplanDto saveTeachplanDto) {
        //参数校验略过
        if (saveTeachplanDto == null) {
            throw new ZhixuePlusException("参数为null");
        }
        //通过参数中的课程ID来判断是新增/修改
        Long id = saveTeachplanDto.getId();
        if (id == null) {
            //新增
            Teachplan teachplan = new Teachplan();
            BeanUtils.copyProperties(saveTeachplanDto, teachplan);
            Long parentid = saveTeachplanDto.getParentid();
            Long courseId = saveTeachplanDto.getCourseId();
            LambdaQueryWrapper<Teachplan> wrapper = new LambdaQueryWrapper<>();
            wrapper = wrapper.eq(Teachplan::getCourseId, courseId).eq(Teachplan::getParentid, parentid);
            Long count = teachplanMapper.selectCount(wrapper);
            teachplan.setOrderby((int) (count + 1));
            teachplanMapper.insert(teachplan);
        } else {
            Teachplan teachplan = teachplanMapper.selectById(id);
            BeanUtils.copyProperties(saveTeachplanDto, teachplan);
            teachplanMapper.updateById(teachplan);
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public R deleteById(Long id) {
        if (id == null) {
            throw new ZhixuePlusException("参数为null");
        }
        Teachplan teachplan = teachplanMapper.selectById(id);
        if (teachplan == null) {
            throw new ZhixuePlusException("查无此数据");
        }
        Long parentid = teachplan.getParentid();
        if ("0".equals(parentid.toString())) {
            LambdaQueryWrapper<Teachplan> wrapper = new LambdaQueryWrapper<>();
            wrapper = wrapper.eq(Teachplan::getParentid, id);
            Long count = teachplanMapper.selectCount(wrapper);
            if (count > 0) {
                return R.fail();
            }
            return teachplanMapper.deleteById(id) > 0 ? R.ok() : R.fail();
        } else {
            int result = teachplanMapper.deleteById(id);
            if (result > 0) {
                mediaMapper.deleteByteachplanId(id);
            }
            return result > 0 ? R.ok() : R.fail();
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void moveDown(Long id) {
        if (id == null) {
            throw new ZhixuePlusException("参数为null");
        }
        Teachplan teachplan = teachplanMapper.selectById(id);
        if (teachplan == null) {
            throw new ZhixuePlusException("查无此数据");
        }
        LambdaQueryWrapper<Teachplan> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper = queryWrapper.eq(Teachplan::getParentid, teachplan.getParentid());
        Long count = teachplanMapper.selectCount(queryWrapper);
        int order = teachplan.getOrderby();
        if (count == order) {
            throw new ZhixuePlusException("已是最后一条，不可向下移动");
        }
        LambdaQueryWrapper<Teachplan> wrapper = new LambdaQueryWrapper<>();
        wrapper = wrapper.eq(Teachplan::getParentid, teachplan.getParentid()).eq(Teachplan::getOrderby, order + 1);
        Teachplan teachplanNext = teachplanMapper.selectOne(wrapper);
        teachplanNext.setOrderby(teachplan.getOrderby());
        teachplan.setOrderby(teachplanNext.getOrderby());
        teachplanMapper.updateById(teachplan);
        teachplanMapper.updateById(teachplanNext);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void moveUp(Long id) {
        if (id == null) {
            throw new ZhixuePlusException("参数为null");
        }
        Teachplan teachplan = teachplanMapper.selectById(id);
        if (teachplan == null) {
            throw new ZhixuePlusException("查无此数据");
        }
        int order = teachplan.getOrderby();
        if (1 == order) {
            throw new ZhixuePlusException("已是第一条，不可向上移动");
        }
        LambdaQueryWrapper<Teachplan> wrapper = new LambdaQueryWrapper<>();
        wrapper = wrapper.eq(Teachplan::getParentid, teachplan.getParentid()).eq(Teachplan::getOrderby, order - 1);
        Teachplan teachplanUp = teachplanMapper.selectOne(wrapper);
        teachplanUp.setOrderby(teachplan.getOrderby());
        teachplan.setOrderby(teachplanUp.getOrderby());
        teachplanMapper.updateById(teachplan);
        teachplanMapper.updateById(teachplanUp);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void associationMedia(BindTeachplanMediaDto bindTeachplanMediaDto) {
        Teachplan teachplan = teachplanMapper.selectById(bindTeachplanMediaDto.getTeachplanId());
        if (teachplan == null) {
            return;
        }
        Integer grade = teachplan.getGrade();
        if (grade != 2) {
            throw new ZhixuePlusException("只允许二级课程绑定媒资");
        }
        //删除原有记录
        LambdaQueryWrapper<TeachplanMedia> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(TeachplanMedia::getTeachplanId, bindTeachplanMediaDto.getTeachplanId());
        mediaMapper.delete(wrapper);
        TeachplanMedia teachplanMedia = new TeachplanMedia();
        BeanUtils.copyProperties(bindTeachplanMediaDto, teachplanMedia);
        teachplanMedia.setMediaFilename(bindTeachplanMediaDto.getFileName());
        teachplanMedia.setCourseId(teachplan.getCourseId());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public R deletePlanMedia(Long teachPlanId, Long mediaId) {
        if (teachPlanId == null || mediaId == null) {
            throw new ZhixuePlusException("参数为null");
        }
        LambdaQueryWrapper<TeachplanMedia> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(TeachplanMedia::getTeachplanId, teachPlanId)
                .eq(TeachplanMedia::getMediaId, mediaId);
        int result = mediaMapper.delete(wrapper);
        return result > 0 ? R.ok() : R.fail();
    }
}
